﻿using ET.DBCommon.DAL;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ET.DBCommon.Model
{
    /// <summary>
    /// 结果场景
    /// </summary>
    public class ExamresultsTestQuestionModel : NotifyPropertyChanged
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 所属竞赛
        /// </summary>
        public int ExamResultsID { get; set; }
        /// <summary>
        /// 试卷场景ID
        /// </summary>
        public int TestQuestionID { get; set; }
        /// <summary>
        /// 场景名称
        /// </summary>
        public string Name { get; set; }

        private decimal m_TotalScore;
        /// <summary>
        /// 场景得分
        /// </summary>
        public decimal TotalScore
        {
            get
            {
                return this.m_TotalScore;
            }
            set
            {
                this.m_TotalScore = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("TotalScore");
            }
        }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 时长
        /// </summary>
        public int CountDown { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Describe { get; set; }
        /// <summary>
        /// 场景文件
        /// </summary>
        public byte[] SceneFile { get; set; }

        public ObservableCollection<ExamresultsTestQuestionTaskModel> m_ExamresultsTestQuestionTasks;
        /// <summary>
        /// 任务列表
        /// </summary>
        public ObservableCollection<ExamresultsTestQuestionTaskModel> ExamresultsTestQuestionTasks
        {
            get { return this.m_ExamresultsTestQuestionTasks; }
            set
            {
                this.m_ExamresultsTestQuestionTasks = value;
                this.OnPropertyChanged("ExamresultsTestQuestionTasks");
            }
        }

        /// <summary>
        /// 列车晚点扣分
        /// </summary>
        private decimal m_TrainLateCount;
        public decimal TrainLateCount
        {
            get
            {
                return this.m_TrainLateCount;
            }
            set
            {
                this.m_TrainLateCount = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("TrainLateCount");
            }
        }

        /// <summary>
        /// 列车迫停扣分
        /// </summary>
        private decimal m_TrainSuspendCount;
        public decimal TrainSuspendCount
        {
            get
            {
                return this.m_TrainSuspendCount;
            }
            set
            {
                this.m_TrainSuspendCount = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("TrainSuspendCount");
            }
        }
        /// <summary>
        /// 作业得分
        /// </summary>
        private decimal m_StepScore;
        public decimal StepScore
        {
            get
            {
                return this.m_StepScore;
            }
            set
            {
                this.m_StepScore = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("StepScore");
            }
        }
        /// <summary>
        /// 记录时间错误扣分
        /// </summary>
        private decimal m_RecordtimeErrorCount;
        public decimal RecordtimeErrorCount
        {
            get
            {
                return this.m_RecordtimeErrorCount;
            }
            set
            {
                this.m_RecordtimeErrorCount = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("RecordtimeErrorCount");
            }
        }
        /// <summary>
        /// 多余步骤个数
        /// </summary>
        private int m_SurplusStep;
        public int SurplusStep
        {
            get
            {
                return this.m_SurplusStep;
            }
            set
            {
                this.m_SurplusStep = value;
                this.OnPropertyChanged("SurplusStep");
            }
        }
        //运行前检查时间
        public static bool PreTime=false;
        //考试时间
        public static bool TestTime=false;
        //查看成绩 add by ljz
        public static bool CheckScore = false;
        /// <summary>
        /// 终止场景的原因
        /// </summary>
        public string StopExplain { get; set; }
        /// <summary>
        /// 场景扣分的原因
        /// </summary>
        public string DeductPointsExplain { get; set; }
        /// <summary>
        /// 场景扣分值
        /// </summary>
        public decimal DeductPointsScore { get; set; }
        /// <summary>
        /// 场景结束时间
        /// </summary>
        private DateTime m_EndTime;
        public DateTime EndTime
        {
            get
            {
                return this.m_EndTime;
            }
            set
            {
                this.m_EndTime = value;
                int minutes = (this.m_EndTime - this.CreateTime).Minutes;
                int seconds = (this.m_EndTime - this.CreateTime).Seconds;
                double TotalSeconds = (this.m_EndTime - this.CreateTime).TotalMilliseconds;
                if (TotalSeconds > 0)
                {

                    this.totalTime = minutes + "分" + seconds + "秒"+TotalSeconds.ToString().Substring(0,1)+"毫秒";
                    this.OnPropertyChanged("TotalTime");
                }
            }
        }

        private string totalTime;


        public string TotalTime {
            get{
                if ((m_EndTime-CreateTime).TotalSeconds>0)
                {
                    String name = ExamresultsTestQuestionDAL.SelectTestquestionName(this.TestQuestionID);
                    if ((!name.Contains("运行"))&& TestTime)
                    {

                        String PreperTime = ExamResultsDAL.SeclectAlltime(this.ExamResultsID); 
                        if (PreperTime != "0" && PreperTime != "")
                        {
                            PreperTime = "00:"+PreperTime.Replace("分", ":").Replace("毫秒", "").Replace("秒", ".");
                            TimeSpan MyDateTime =new TimeSpan(Convert.ToDateTime("1990-01-01 " + PreperTime).Ticks);
                            TimeSpan otherTime = (this.m_EndTime - this.CreateTime).Duration();
                            TimeSpan _Total = new TimeSpan(otherTime.Ticks + MyDateTime.Ticks);
                            if(!CheckScore)
                                ExamResultsDAL.UpdateExamResultsTime(_Total.Minutes.ToString("D2")+"分"+ _Total.Seconds.ToString("D2") +"秒"+ _Total.Milliseconds.ToString().Substring(0,1)+"毫秒", this.ExamResultsID);
                        }
                        else
                        {
                            TimeSpan _Total = (this.m_EndTime - this.CreateTime).Duration();
                            if (!CheckScore)
                                ExamResultsDAL.UpdateExamResultsTime(_Total.Minutes.ToString("D2") + "分" + _Total.Seconds.ToString("D2") + "秒" + _Total.Milliseconds.ToString().Substring(0, 1) + "毫秒", this.ExamResultsID);
                        }

                    }
                    else
                    {
                        if (PreTime)
                        {
                            TimeSpan _Total = (this.m_EndTime - this.CreateTime).Duration();
                            if (!CheckScore)
                                ExamResultsDAL.UpdateExamResultsTime1(_Total.Minutes.ToString("D2") + "分" + _Total.Seconds.ToString("D2") + "秒" + _Total.Milliseconds.ToString().Substring(0, 1) + "毫秒", this.ExamResultsID);
                        }

                    }
                    TimeSpan _Total2 = (this.m_EndTime - this.CreateTime).Duration();
                    return _Total2.Minutes.ToString("D2") + "分" + _Total2.Seconds.ToString("D2") + "秒" + _Total2.Milliseconds.ToString().Substring(0, 1) + "毫秒";
                }
                else
                {
                    return "";
                }
                
            }
            set {
                totalTime = value;
            }

        }

        /// <summary>
        /// 是否主场景
        /// </summary>
        public bool IsMain { get; set; }
        /// <summary>
        /// 副场景集合
        /// </summary>
        public string AssistantQuestions { get; set; }

        /// <summary>
        /// 场景注入状态
        /// </summary>
        public bool InjectionSceneState { get; set; }
        /// <summary>
        /// 扣分参数
        /// </summary>
        public string MinusPoints { get; set; }

        /// <summary>
        /// 车站ID,CCTV加载场景默认显示车站ID
        /// </summary>
        public int StationID { get; set; }
        /// <summary>
        /// 故障场景发生 汇报倒计时，例如道岔发生 60S时长调度员发现时间，否则故障自动汇报
        /// </summary>
        public int WaitCountDown { get; set; }
        /// <summary>
        /// 停运晚点总分
        /// </summary>
        private decimal m_StopLaterScore;

        private decimal m_TestPaperTotal;
        public decimal StopLaterScore
        {
            get
            {
                return this.m_StopLaterScore;
            }
            set
            {
                this.m_StopLaterScore = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("StopLaterScore");
            }
        }
        /// <summary>
        /// 提交元素总分
        /// </summary>
        private decimal m_SubmitElementScore;
        public decimal SubmitElementScore
        {
            get
            {
                return this.m_SubmitElementScore;
            }
            set
            {
                this.m_SubmitElementScore = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("SubmitElementScore");
            }
        }

        /// <summary>
        /// 列车停运扣分
        /// </summary>
        private decimal m_StopsRunningCount;
        public decimal StopsRunningCount
        {
            get
            {
                return this.m_StopsRunningCount;
            }
            set
            {
                this.m_StopsRunningCount = decimal.Parse(value.ToString("F2"));
                this.OnPropertyChanged("StopsRunningCount");
            }
        }

        /// <summary>
        /// 停运车次
        /// </summary>
        private string m_StopsRunningTrain;
        public string StopsRunningTrain
        {
            get
            {
                return this.m_StopsRunningTrain;
            }
            set
            {
                this.m_StopsRunningTrain = value;
                this.OnPropertyChanged("StopsRunningTrain");
            }
        }
        public decimal TestPaperTotal{ get; set; }
    }
}
